#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <malloc.h>
#include <string.h>

#include "login.h"
#include "getch.h"
#include "game_function.h"

/*
 *@brief        读取用户名,密码,金币,积分
 */
struct data *read_file()
{
    struct data *p_data, *temp_one, *temp_two;
    p_data = (struct data *)malloc(sizeof(struct data));
    temp_one = p_data;
    temp_two = temp_one;
    p_data->next = NULL;

    FILE *read;
    read = fopen("./lib/login.txt", "a+");
    if (read == NULL) {
        printf("无法打开文件");
        fclose(read);
        exit (0);
    }

    if (4 != fscanf(read, "%s%s%d%d", p_data->name, p_data->passwd,
                                      &p_data->grade, &p_data->gold)) {
        free(p_data);
        fclose(read);
        p_data = NULL;
        return p_data;
    }
    temp_one = (struct data *)malloc(sizeof(struct data));
    temp_two->next = temp_one;

    while (1) {
        if (4 != fscanf(read, "%s%s%d%d", temp_one->name, temp_one->passwd,
                                          &temp_one->grade, &temp_one->gold)) {
            free(temp_one);
            temp_two->next = NULL;
            break;
        }

        temp_two = temp_one;
        temp_one = (struct data *)malloc(sizeof(struct data));
        temp_two->next = temp_one;
    }
        fclose(read);

        return p_data;
}

/*
 *@brief        将链表的数据写入文件
 */
void write_to_file(struct data *p_data)
{
    if (!p_data) {
        printf("ERROR!");
        return;
    }
    
    FILE* save_file;

    save_file = fopen("./lib/login.txt", "w");
    
    /*将p_data指向头指针，从头到尾依次将每个节点的数据写入文件*/
    while (p_data) {
		fprintf(save_file, "%s\t%s\t%d\t%d\r\n", p_data->name, p_data->passwd,
                                                 p_data->grade, p_data->gold);
		p_data = p_data->next;
    }
    
    fclose(save_file);
}

/*
 *@brief        登陆用户
 */
char *login(struct data *p_data) 
{
    struct data *temp_one;
    char static name[20], passwd[20];
    int code_num;
    temp_one = p_data;
   
    SHOW_CURSOR();

    printf("输入完毕按Enter确认...\n\n");
    printf("请输入用户名：");
    scanf("%s", name);
    printf("\n");
    printf("请输入密码：");
    getchar();

    for (code_num = 0; code_num < 10; code_num++) {
        passwd[code_num] = getch();
        if (passwd[code_num] == '\r')
        break;
        printf("*");
    }
    printf("\n");
    passwd[code_num] = '\0';

    while (temp_one) {
        if (0 == strcmp(temp_one->name, name)
            && 0 == strcmp(temp_one->passwd, passwd)) {
            printf("\n");
            printf("登陆成功！\n");
            printf("任意键继续...");
            getch();

            return name;
        }
        temp_one = temp_one->next;
    }
   
    printf("\n");
    printf("账号密码错误,或者账号不存在,可注册账号。\n");
    printf("按任意键返回...");
    getch();

    return NULL;
}

/*
 *@brief        注册用户
 */
void sign_in(struct data *p_data)
{
    struct data *temp_one, *temp_two;
    FILE *write;
    char name[20], passwd[20];
    int grade = 0, gold = 2000;

    SHOW_CURSOR();

    if (p_data == NULL) {
        p_data = (struct data *)malloc(sizeof(struct data));
        p_data->next = NULL;

        printf("输入完毕按Enter键确认\n\n");
        printf("请输入用户名：");
        scanf("%s", p_data->name);
        printf("\n");
        printf("请输入密码：");
        printf("\n");
        scanf("%s", p_data->passwd);
        getchar();
        printf("注册成功!\n");
        printf("任意键返回...");
        
        write = fopen("./lib/login.txt", "a");
        if (write == NULL) {
            printf("不能打开文件！");
            fclose(write);
            exit (0);
        }

        fprintf(write, "%s\t%s\t%d\t%d\n", p_data->name,p_data->passwd,
                                           grade, gold);
        fclose(write);

        return;
    }

    temp_one = p_data;

    while (1) {
        temp_two = temp_one;
        temp_one = temp_two->next;
        if (!temp_one) {
            temp_one = (struct data *)malloc(sizeof(struct data));
            temp_two->next = temp_one;

            printf("输入完毕按Enter键确认\n\n");
            printf("请输入用户名：");
            scanf("%s", name);

            printf("\n");
            printf("请输入密码：");
            scanf("%s", passwd);
            printf("\n");
            getchar();
            printf("注册成功！\n");
            printf("按任意键返回...\n");

            strcpy(temp_one->name, name);
            strcpy(temp_one->passwd, passwd);
            temp_one->grade = 0;
            temp_one->gold = 2000;

            write = fopen("./lib/login.txt", "a");
            if (write == NULL) {
                printf("不能打开文件");
                fclose(write);
                exit (0);
            }

            fprintf(write, "%s\t%s\t%d\t%d\n", temp_one->name,temp_one->passwd,
                                               grade, gold);
            fclose(write);
            break;
        }
    }
    
    HIDE_CURSOR();

    return;
}
